package com.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.bean.Admin;
import com.order.common.entiry.ResultCode;
import com.order.common.entiry.RoleEnum;
import com.order.common.exception.CommonException;
import com.order.common.util.SqlSessionFactoryUtil;
import com.order.mapper.AdminMapper;
import com.order.service.AdminService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminServiceImpl implements AdminService {

    private AdminMapper adminMapper;

    private SqlSession sqlSession;

    private void closeMapper() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    private void initMapper() {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();
        SqlSession sqlSession = factory.openSession(true);
        adminMapper = sqlSession.getMapper(AdminMapper.class);
    }


    @Override
    public Admin login(Admin admin) {
        initMapper();
        String username = admin.getUsername();
        boolean flag = true;
        Admin dbAdmin = adminMapper.selectByUsername(username);
        if (dbAdmin == null) {
            flag = false;
            new CommonException(ResultCode.ACCOUNT_NOT_FOUND);
        } else if (!dbAdmin.getPassword().equals(admin.getPassword())) {
            flag = false;
            new CommonException(ResultCode.UNMOBILE_OR_PASSWORD);
        }
        closeMapper();
        return dbAdmin;
    }

    @Override
    public boolean addAdmin(Admin admin) {
        initMapper();
        Admin dbAdmin = adminMapper.selectByUsername(admin.getUsername());
        if (dbAdmin != null) {
            new CommonException(ResultCode.ACCOUNT_ALREADY_EXISTS);
        } else if (admin.getPassword() == null) {
            new CommonException(ResultCode.PASSWORD_CANNOT_BE_EMPTY);
        }
        if (admin.getUsername() == null) {
            admin.setName(admin.getUsername());
        }
        admin.setRole(RoleEnum.ADMIN.name());
        boolean ins = adminMapper.insert(admin);
        closeMapper();
        return ins;
    }

    @Override
    public boolean updateById(Admin admin) {
        initMapper();
        boolean up = adminMapper.updateById(admin);
        closeMapper();
        return up;
    }

    @Override
    public boolean deleteById(Integer id) {
        initMapper();
        boolean del = adminMapper.deleteById(id);
        closeMapper();
        return del;
    }

    @Override
    public boolean deleteBatch(Integer[] ids) {
        for (Integer id :ids){
            boolean b = this.deleteById(id);
            if (!b) return b;
        }
        return true;
    }

    @Override
    public Admin selectById(int id) {
        initMapper();
        Admin dbAdmin = adminMapper.selectById(id);
        closeMapper();
        return dbAdmin;
    }

    @Override
    public List<Admin> selectAll(String name) {
        initMapper();
        List<Admin> dbAdmins = adminMapper.selectAll(name);
        closeMapper();
        return dbAdmins;
    }

    @Override
    public PageInfo<Admin> selectPage(String name, Integer pageNum, Integer pageSize) {
        initMapper();
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> dbAdmins = this.selectAll(name);
        closeMapper();
        return PageInfo.of(dbAdmins);
    }
}
